package com.ultrawise.survey.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ReflectionUtils {

	/*
	 * 注意field的访问问题
	 */
	public static void setFieldVlaue(Object obj, String fieldName, Object value) {
		Class c = obj.getClass();
		try {
			Field field;
			try {
				field = c.getDeclaredField(fieldName);
				field.setAccessible(true);
			} catch (NoSuchFieldException e) {
				return;
			}
			field.set(obj, value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Object getFieldVlaue(Object obj, String fieldName,
			Object value) {
		Class c = obj.getClass();
		try {
			Field field;
			try {
				field = c.getDeclaredField(fieldName);
				field.setAccessible(true);
			} catch (NoSuchFieldException e) {
				return null;
			}
			return field.get(obj);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static List findNotNullField(Object obj) {
		Class c = obj.getClass();
		List list = new ArrayList(10);
		try {
			Field[] fields = c.getDeclaredFields();
			for(int i=0;i<fields.length;i++){
				fields[i].setAccessible(true);
				if(fields[i].get(obj)!=null){
					list.add(fields[i]);
				}
				
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}
	
	public static List findAllField(Object obj) {
		Class c = obj.getClass();
		List list = new ArrayList(10);
		try {
			Field[] fields = c.getDeclaredFields();
			for(int i=0;i<fields.length;i++){
				fields[i].setAccessible(true);
				if(!fields[i].getName().equalsIgnoreCase("caseLink"))
				{
					list.add(fields[i]);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}
}
